package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Component
@Slf4j
//定时任务

public class MYTASK {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 处理支付超时订单
     */
    @Scheduled(cron = "5 * * * * ?")
    public void processTimeoutOrder(){
        log.info("处理支付超时订单：{}",new Date());

        //获取当前时间并且-15分，订单开始时间<当前时间-15分则为超时订单
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        String reason = "超时未支付，系统自动取消";

        //查询超时订单,将状态改为“已取消”，传入当前时间，需要修改的状态
        orderMapper.processTimeoutOrder(time,reason,Orders.CANCELLED);
    }

    /**
     * 处理“派送中”状态的订单
     */
    @Scheduled(cron = "1 10 12 * * ?")
    public void processDeliveryOrder(){
        //每天0点自动将“派送中”状态的订单改为“已完成”
        log.info("处理派送中订单：{}",new Date());
        orderMapper.processDeliveryOrder(Orders.COMPLETED,1);

    }
}